//
//  MainTabBarController.m
//  ASIRequestDemo
//
//  Created by 1 on 16/1/4.
//
//

#import "MainTabBarController.h"
#import "DianPuViewController.h"
#import "DingDanViewController.h"
#import "ShangPinViewController.h"
#import "WoDeViewController.h"
#import "MainTabBarItem.h"
#import "BaseNavigationViewController.h"

@interface MainTabBarController ()

@end
@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initSubViewControllers];
    [self _customTabBar];

}
//定制自己的tabBar
- (void)_customTabBar {
    //移除tabBar原有的视图
    for (UIView * subView in self.tabBar.subviews) {
        [subView removeFromSuperview];
    }
    //tabBar的背景
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"标签栏.png"]];
    //按钮的所有图片
    NSArray * imagesArray = @[@"店铺.png",@"订单.png",@"商品.png",@"我的.png"];
    //按钮的title
    NSArray * titleArray = @[@"店铺",@"订单",@"商品",@"我的"];
    
    
    //tabBar按钮的大小
    CGFloat tabBarButtonWidth = [UIScreen mainScreen].bounds.size.width / imagesArray.count;
    
    //创建按钮
    for (int i = 0; i < imagesArray.count; i++) {
        MainTabBarItem * item = [[MainTabBarItem alloc] initWithFrame:CGRectMake(i * tabBarButtonWidth, 0, tabBarButtonWidth, 49)];
        
        item.tag = i + 100;
        
        item.tabBarTitleName = titleArray[i];
        item.tabBarImageName = imagesArray[i];
        //添加事件
        [item addTarget:self action:@selector(itemAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.tabBar addSubview:item];
        
        
    }
}
//创建子视图控制器
- (void)_initSubViewControllers {
    //首页
    DianPuViewController * homeVC = [[DianPuViewController alloc] init];
    
    //订单
    DingDanViewController * newsVC = [[DingDanViewController alloc] init];
    
    //发现
    ShangPinViewController * topVC = [[ShangPinViewController alloc] init];
    
    //我的
    WoDeViewController * cinemaVC = [[WoDeViewController alloc] init];
    
    //视图控制器数组
    NSArray * subViewControllersArray = @[homeVC,newsVC,topVC,cinemaVC];
    
    //导航器数组
    NSMutableArray * baseNavigationArray = [NSMutableArray arrayWithCapacity:1];
    
    for (UIViewController * vc in subViewControllersArray) {
        
        //创建
        BaseNavigationViewController * baseNavigationViewController = [[BaseNavigationViewController alloc] initWithRootViewController:vc];
        
        //添加
        [baseNavigationArray addObject:baseNavigationViewController];
        
        
    }
    
    //将导航器数组加入到tabBar
    self.viewControllers = baseNavigationArray;
    
    
}
#pragma mark item的按钮事件
- (void)itemAction:(UIButton *) sender {
    
    //跳转到相应的视图控制器
    self.selectedIndex = sender.tag - 100;
    
}

@end
